package com.example.musicplayer.dialogTools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.musicTools.Lyric;

public class LyricSizeSetDialog extends BaseDialog implements View.OnClickListener {
    private int mSelected;
    private final int mSelectedColor;
    private final int mUnSelectedColor;
    private LyricSizeChangeListener mListener;

    public LyricSizeSetDialog(Context context) {
        super(context, R.style.NormalDialogStyle_Bottom);
        updateSelected();
        mSelectedColor = context.getResources().getColor(R.color.spring_green);
        mUnSelectedColor = context.getResources().getColor(R.color.black);
        clickToChangeSize(mSelected);
    }

    @Override
    protected View setDialogView(Context context) {
        View view = View.inflate(context, R.layout.dialog_lyric_size_set, null);
        view.findViewById(R.id.complete).setOnClickListener(this);
        view.findViewById(R.id.lyric_size_1).setOnClickListener(this);
        view.findViewById(R.id.lyric_size_2).setOnClickListener(this);
        view.findViewById(R.id.lyric_size_3).setOnClickListener(this);
        view.findViewById(R.id.lyric_size_4).setOnClickListener(this);
        view.findViewById(R.id.lyric_size_5).setOnClickListener(this);
        return view;
    }

    @Override
    protected void setDialogLayout(Context context) {
        Window dialogWindow = mDialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = (int) context.getResources().getDimension(R.dimen.lyric_size_set_dialog_height);
            lp.gravity = Gravity.BOTTOM;
            dialogWindow.setAttributes(lp);
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete:
                dismiss();
                break;
            case R.id.lyric_size_1:
                Lyric.setLyricSize(Lyric.LYRIC_SIZE_1);
                clickToChangeSize(R.id.lyric_size_1);
                break;
            case R.id.lyric_size_2:
                Lyric.setLyricSize(Lyric.LYRIC_SIZE_2);
                clickToChangeSize(R.id.lyric_size_2);
                break;
            case R.id.lyric_size_3:
                Lyric.setLyricSize(Lyric.LYRIC_SIZE_3);
                clickToChangeSize(R.id.lyric_size_3);
                break;
            case R.id.lyric_size_4:
                Lyric.setLyricSize(Lyric.LYRIC_SIZE_4);
                clickToChangeSize(R.id.lyric_size_4);
                break;
            case R.id.lyric_size_5:
                Lyric.setLyricSize(Lyric.LYRIC_SIZE_5);
                clickToChangeSize(R.id.lyric_size_5);
                break;
            default:
                break;
        }
    }

    private void clickToChangeSize(int id) {
        TextView currentView = getDialogView().findViewById(id);
        if (currentView != null) {
            currentView.setTextColor(mSelectedColor);
        }
        if (mSelected != id) {
            TextView preView = getDialogView().findViewById(mSelected);
            if (preView != null) {
                preView.setTextColor(mUnSelectedColor);
            }
            mSelected = id;
            if (mListener != null) {
                mListener.onLyricSizeChange();
            }
        }
    }

    private void updateSelected() {
        switch (Lyric.getLyricSize()) {
            case Lyric.LYRIC_SIZE_1:
                mSelected = R.id.lyric_size_1;
                break;
            case Lyric.LYRIC_SIZE_2:
                mSelected = R.id.lyric_size_2;
                break;
            case Lyric.LYRIC_SIZE_4:
                mSelected = R.id.lyric_size_4;
                break;
            case Lyric.LYRIC_SIZE_5:
                mSelected = R.id.lyric_size_5;
                break;
            case Lyric.LYRIC_SIZE_3:
            default:
                mSelected = R.id.lyric_size_3;
                break;
        }
    }

    public void setOnLyricSizeChangeListener(LyricSizeChangeListener listener) {
        mListener = listener;
    }

    public interface LyricSizeChangeListener {
        void onLyricSizeChange();
    }
}